﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, UserRoleDto, UsingFormDetailExeInput } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { TestDataDto,UsingFormServiceProxy,UsingFormState,RoleUserServiceProxy } from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'requirementGetTabEditComponent',
    templateUrl: './requirement-get-tab_edit.component.html'
})
export class RequirementGetTabEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    canChangeUserName = true;
    isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
    isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();

    usingFormDto: TestDataDto = new TestDataDto();
    roles: UserRoleDto[];
    sendActivationEmail = true;
    setRandomPassword = true;
    passwordComplexityInfo = '';
    profilePicture: string;

    allOrganizationUnits: OrganizationUnitDto[];
    memberedOrganizationUnits: string[];
    editFlag = true;
    usingFormDtoStateOption: object[];// 审核状态
    usingFormId: number;
    userId: string;
    state: number;
    authorityUserOption:Array<object>;

    constructor(
        injector: Injector,
        private _usingFormServiceProxy: UsingFormServiceProxy,
        private _roleUserServiceProxy: RoleUserServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.usingFormDtoStateOption = new Array();
        this.usingFormDtoStateOption.push({ value: 0, text: UsingFormState[0] });
        this.usingFormDtoStateOption.push({ value: 1, text: UsingFormState[1] });
        this.usingFormDtoStateOption.push({ value: 2, text: UsingFormState[2] });
        this.usingFormDtoStateOption.push({ value: 3, text: UsingFormState[3] });
    }
    /**
     * 获取有权限的用户
     */
    getAuthorityUserOption(_pState) {
        this.authorityUserOption = new Array<object>()
        //生产处审批人 = 1,
        this._roleUserServiceProxy.getUsersByRole(_pState)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.authorityUserOption = result;
            });
    }
    show(_pUsingFormId: number): void {
        this.usingFormId = _pUsingFormId;
        this.userId = "";
        // 审核  toCheck相关人
        this.getAuthorityUserOption(1);
        this.active = true;
        this.modal.show();
    }

    getProfilePicture(profilePictureId: string): void {
        if (!profilePictureId) {
            this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png';
        } else {
            this._profileService.getProfilePictureById(profilePictureId).subscribe(result => {

                if (result && result.profilePicture) {
                    this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture;
                } else {
                    this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png';
                }
            });
        }
    }

    onShown(): void {
        document.getElementById('userid').focus();
    }
    toCheck(): void {
        this.message.confirm(
            '是否确认提交？',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    var usingFormDetailExeInput = new UsingFormDetailExeInput();
                    usingFormDetailExeInput.id = this.usingFormId;
                    usingFormDetailExeInput.userId = this.userId;
                    // 审批
                    this._usingFormServiceProxy.toExamine(usingFormDetailExeInput)
                        .pipe(finalize(() => { }))
                        .subscribe(() => {
                            this.notify.info(this.l('SavedSuccessfully'));
                            this.close();
                            this.modalSave.emit(null);
                        });
                }
            }
        );
    }
        /**
     * 需求领用出库
     * @param record 
     */
    toExecuting() {
        
    }
        /**
     * 完成申请单
     * @param record 
     */
    toFinish() {

    }
    save(): void {
        this.toCheck();
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
